﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SoccerScore.Compact.Linq.Review;
using SoccerScore.Compact.Linq;
using DevExpress.XtraCharts;
using System.Reflection;

namespace Soccer_Score_Forecast
{
    public partial class Form1 : Form
    {
        //过滤不让球

        //过滤 wdl，310，

        int overday = -2;


        private List<string> filter_match = new List<string>();
        private List<string> filter_match_two = new List<string>();
        private List<string> todayMatchType = new List<string>();
        private List<string> todayMatchType_two = new List<string>();
        private Dictionary<string, double> matchtype_rate;
        private Dictionary<string, double> matchtype_rate_two;
        private int rate_threshold_one = 100;
        private int rate_threshold_two = 50;


        //测试函数的正确运行
        //2012.7.28
        private void testRunMatchTypeRate_two(string tt)
        {
            PredictAnalysisAfter paa = new PredictAnalysisAfter();
            double y = 0;
            //using (DataClassesMatchDataContext2 matches = new DataClassesMatchDataContext2(Conn.conn))
            //{
            //var ar = from p in matches.Analysis_Review.Where(p => p.Result_wdl != null).AsParallel()
            //         where p.bj_select == 2
            //         //where p.result_grnn != null
            //         where p.Match_type == tt
            //         //where todayMatchType_two.Contains(p.Match_type)

            //         where p.Result_wdl.Contains("W") || p.Result_wdl.Contains("L")
            //         //where p.Result_wdl != null
            //         //where p.Result_goals =="30"
            //         //where p.Result_goals.Length != 3
            //         where p.Data_wdl.Length != 3
            //         //
            var ar = from p in paa.GetPredictTwo_Match(tt)
                     group p by new { Match_time = p.Match_time.Value.Date, p.Match_type } into ttt
                     select new
                     {
                         ddate = ttt.Key.Match_time,
                         ttype = ttt.Key.Match_type,
                         //wdlsuccrate = 100.0 * ttt.Where(e => e.Result_wdl.Contains("W")).Count() / ttt.Count(),
                         wdlsuccrate = 100.0 * ttt.Where(e => e.result_grnn.Contains("yes")).Count() / ttt.Count(),
                         // wdlsuccrate = 100 * ttt.Where(e => e.result_grnn.Contains("yes")).Count()
                     };
            //var tables = from p in matches.Analysis_Review.Where(p => p.Result_wdl != null).AsParallel()
            //             where p.bj_select == 2
            //             //where p.result_grnn != null
            //             where p.Match_type == tt
            //             //where todayMatchType_two.Contains(p.Match_type)

            //             where p.Result_wdl.Contains("W") || p.Result_wdl.Contains("L")
            //             //where p.Result_wdl != null
            //             //where p.Result_goals =="30"
            //             //where p.Result_goals.Length != 3
            //             where p.Data_wdl.Length != 3
            //             orderby p.Match_time
            var tables = from p in paa.GetPredictTwo_Match(tt)
                         select p;

            gridControl1.DataSource = tables.ToList();
            button7.PerformClick();

            List<MatchPoint<double>> CurveFit = new List<MatchPoint<double>>();
            var ar_order = ar.Where(e => e.ttype == tt).OrderBy(e => e.ddate);
            var xmin = ar.Min(e => e.ddate);
            var xd = (DateTime.Now.Date - xmin).Days;
            foreach (var item in ar_order)
            {
                MatchPoint<double> m = new MatchPoint<double>();
                m.LastMatchWDL = item.wdlsuccrate;
                m.matchTime = item.ddate;
                m.LastMatchOverTime = (item.ddate - xmin).Days;
                CurveFit.Add(m);
            }
            if (CurveFit.Count > 5)
            {
                var ylist = paa.ployfitSeries(CurveFit, xd);//这里做预测

                y = ylist.OrderByDescending(e => e.LastMatchOverTime)
                   .Select(e => e.LastMatchWDL).First();

            }


            chartControl1.Series.Clear();
            Series series1 = new Series("testRunMatchTypeRate", ViewType.Bar);
            foreach (var m in CurveFit)
                series1.Points.Add(new SeriesPoint(m.LastMatchOverTime, m.LastMatchWDL));
            series1.Points.Add(new SeriesPoint(xd, y));
            chartControl1.Series.Add(series1);
            series1.Label.Visible = true;

            XYDiagram diagram = (XYDiagram)chartControl1.Diagram;
            diagram.AxisY.LogarithmicBase = 2;
            diagram.AxisY.Logarithmic = true;
            //}
        }

        private void RunMatchTypeRate_two()
        {
            PredictAnalysisAfter paa = new PredictAnalysisAfter();
            matchtype_rate_two = new Dictionary<string, double>();

            //using (DataClassesMatchDataContext2 matches = new DataClassesMatchDataContext2(Conn.conn))
            //{
            //var ar = from p in matches.Analysis_Review.Where(p => p.Result_wdl != null).AsParallel()
            //         where p.bj_select == 2
            //         //where p.result_grnn != null

            //         where todayMatchType_two.Contains(p.Match_type)

            //         where p.Result_wdl.Contains("W") || p.Result_wdl.Contains("L")
            //         //where p.Result_wdl != null
            //         //where p.Result_goals =="30"
            //         //where p.Result_goals.Length != 3
            //         where p.Data_wdl.Length != 3
            //
            var ar = from p in paa.GetPredictTwo()
                     group p by new { Match_time = p.Match_time.Value.Date, p.Match_type } into ttt
                     select new
                     {
                         ddate = ttt.Key.Match_time,
                         ttype = ttt.Key.Match_type,
                         //wdlsuccrate = 100.0 * ttt.Where(e => e.Result_wdl.Contains("W")).Count() / ttt.Count(),
                         wdlsuccrate = 100.0 * ttt.Where(e => e.result_grnn.Contains("yes")).Count() / ttt.Count(),
                         //wdlsuccrate = 100 * ttt.Where(e => e.result_grnn.Contains("yes")).Count()
                     };

            //var artype = matches.Analysis_Review.AsParallel()
            //    //.Where(e => e.result_grnn == null)
            //    //.Where(e => e.Result_wdl == null
            //    //.Where(e=>!e.Result_wdl.Contains("W") && !!e.Result_wdl.Contains("L"))
            //.Where(e => e.full_home_goals == null)
            //.Select(e => e.Match_type).Distinct();



            //if (todayMatchType == null) todayAnalysisReview(false);

            //todayAnalysisReview(2, false);

            foreach (var tt in paa.GetTodayMatch())
            {

                if (todayMatchType_two.Count > 0)
                    if (todayMatchType_two.Contains(tt))
                    {
                        List<MatchPoint<double>> CurveFit = new List<MatchPoint<double>>();
                        var ar_order = ar.Where(e => e.ttype == tt).OrderBy(e => e.ddate);
                        var xmin = ar.Min(e => e.ddate);
                        var xd = (DateTime.Now.Date - xmin).Days;
                        foreach (var item in ar_order)
                        {
                            MatchPoint<double> m = new MatchPoint<double>();
                            m.LastMatchWDL = item.wdlsuccrate;
                            m.matchTime = item.ddate;
                            m.LastMatchOverTime = (item.ddate - xmin).Days;
                            CurveFit.Add(m);
                        }
                        if (CurveFit.Count > 5)
                        {
                            var ylist = paa.ployfitSeries(CurveFit, xd);//这里做预测

                            var y = ylist.OrderByDescending(e => e.LastMatchOverTime)
                                .Select(e => e.LastMatchWDL).First();

                            matchtype_rate_two.Add(tt, y);

                        }
                    }
            }
            //}
        }


        private void btn_today_two_Click(object sender, EventArgs ee)
        {
            PredictAnalysisAfter paa = new PredictAnalysisAfter();
            //IEnumerable<Analysis_Review> tables;
            //using (DataClassesMatchDataContext2 matches = new DataClassesMatchDataContext2(Conn.conn))
            //{

            //tables = matches.Analysis_Review
            //            .Where(e => e.Match_time.Value > DateTime.Now.AddDays(overday))
            //    //.Where(e => e.Result_goals.Length == 3)  //只有2个结果，修改成3个结果的
            //      .Where(e => e.Data_wdl.Length != 3) //不等于3
            //            .Where(e => e.bj_select == 2)  //非让球的
            //            .OrderBy(e => e.Match_time);

            var tables = from p in paa.GetPredictTwo_Today(overday)
                         select p;
            todayMatchType_two = new List<string>();
            todayMatchType_two = tables.Select(e => e.Match_type).Distinct().ToList();

            gridControl1.DataSource = tables.ToList();
            //gridControl1.Refresh();
            //gridView1.PopulateColumns();

            //DevExpress.XtraGrid.Columns.GridColumn col = gridView1.Columns[19];
            //col.DisplayFormat.FormatString = "yyyy-MM-dd HH:mm:ss";
            //col.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom;
            //gridView1.OptionsView.ColumnAutoWidth = false;
            //gridView1.BestFitColumns();
            ////设置并制作Foot下面的统计信息
            //gridView1.Columns[19].SummaryItem.DisplayFormat = "{0}";
            ////gridView1.Columns[0].SummaryItem.FieldName = "analysis_review_id";
            //gridView1.Columns[19].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Count;

            button7.PerformClick();
            //}
        }

        private void btn_last_two_Click(object sender, EventArgs ee)
        {
            RunMatchTypeRate_two();
            chartControl1.Series.Clear();
            Series series1 = new Series("wdlsucc", ViewType.Bar);
            foreach (var m in matchtype_rate_two)
            {
                //if (m.Value > 0)
                series1.Points.Add(new SeriesPoint(m.Key, m.Value));
                if (m.Value > rate_threshold_two)
                    filter_match_two.Add(m.Key);
            }
            chartControl1.Series.Add(series1);
            series1.Label.Visible = true;

            XYDiagram diagram = (XYDiagram)chartControl1.Diagram;
            diagram.AxisY.LogarithmicBase = 2;
            diagram.AxisY.Logarithmic = true;

        }

        private void btn_filter_two_Click(object sender, EventArgs ee)
        {
            PredictAnalysisAfter paa = new PredictAnalysisAfter();
            //getBjDangChang();
            //DataClassesMatchDataContext2 matches = new DataClassesMatchDataContext2(Conn.conn);
            //var tables = matches.Analysis_Review
            //                   .Where(e => e.Match_time.Value > DateTime.Now.AddDays(overday))
            //                    .Where(e => filter_match_two.Contains(e.Match_type))  //上次赢了的类型
            //    //.Where(e => bjdanchang.Contains(e.Match_type)) //出错率大于50的类型
            //    //.Where(e=>e.Result_goals=="30") 
            //     .Where(e => e.Data_wdl.Length != 3) //不等于3
            //                   .Where(e => e.bj_select == 2)
            //                   .OrderBy(e => e.Match_time);

            var tables = from p in paa.GetPredictTwo_TodayFilter(overday, filter_match_two)
                         select p;
            //todayMatchType = new List<string>();
            //todayMatchType = tables.Select(e => e.Match_type).Distinct().ToList();

            gridControl1.DataSource = tables.ToList();
            //gridControl1.Refresh();
            //gridView1.PopulateColumns();

            //DevExpress.XtraGrid.Columns.GridColumn col = gridView1.Columns[19];
            //col.DisplayFormat.FormatString = "yyyy-MM-dd HH:mm:ss";
            //col.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom;
            //gridView1.OptionsView.ColumnAutoWidth = false;
            //gridView1.BestFitColumns();


            button7.PerformClick();

            ///*
            //for (int i = 0; i < 10; i++) gridView1.Columns[i].Visible = false;
            //for (int i = 11; i < 18; i++) gridView1.Columns[i].Visible = false;
            //for (int i = 23; i < 28; i++) gridView1.Columns[i].Visible = false;
            //**/

            ////设置并制作Foot下面的统计信息
            //gridView1.Columns[19].SummaryItem.DisplayFormat = "{0}";
            ////gridView1.Columns[0].SummaryItem.FieldName = "analysis_review_id";
            //gridView1.Columns[19].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Count;
        }

        private void btn_send_two_Click(object sender, EventArgs e)
        {
            if (SendPredictEmail.SendEmail("jc777777@163.com", "jc777",
     "jc777777@163.com", "jc777",
     DateTime.Now.ToLongDateString(), get_today_predict_ab(), false, null,
     "smtp.163.com", "jc777777", "123qweasd"))
                //MessageBox.Show("Email sent successfully");
                toolStripStatusLabel1.Text = "Email sent successfully";
            else
                //MessageBox.Show("Failed to send email");
                toolStripStatusLabel1.Text = "Failed to send email";
        }


        private void button14_Click(object sender, EventArgs ee)
        {
            IEnumerable<Analysis_Review> tables;
            using (DataClassesMatchDataContext2 matches = new DataClassesMatchDataContext2(Conn.conn))
            {

                tables = matches.Analysis_Review
                    // .Where(e => e.Match_time.Value > DateTime.Now.AddDays(overday))
                    //.Where(e => e.Result_goals.Length == 3)  //只有2个结果，修改成3个结果的
                    //.Where(e => e.Data_wdl.Length != 3) //不等于3
                            .Where(e => e.bj_select == 2)  //非让球的
                            .OrderBy(e => e.Match_time);

                //todayMatchType_two = new List<string>();
                todayMatchType_two = tables.Select(e => e.Match_type).Distinct().ToList();

                gridControl1.DataSource = tables;
                //gridControl1.Refresh();
                //gridView1.PopulateColumns();

                //DevExpress.XtraGrid.Columns.GridColumn col = gridView1.Columns[19];
                //col.DisplayFormat.FormatString = "yyyy-MM-dd HH:mm:ss";
                //col.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom;
                //gridView1.OptionsView.ColumnAutoWidth = false;
                //gridView1.BestFitColumns();
                ////设置并制作Foot下面的统计信息
                //gridView1.Columns[19].SummaryItem.DisplayFormat = "{0}";
                ////gridView1.Columns[0].SummaryItem.FieldName = "analysis_review_id";
                //gridView1.Columns[19].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Count;

                ///*
                //for (int i = 0; i < 10; i++) gridView1.Columns[i].Visible = false;
                //for (int i = 17; i < 18; i++) gridView1.Columns[i].Visible = false;
                // * */

                button7.PerformClick();

            }
        }


        private void button7_Click(object sender, EventArgs e)
        {
            gridControl1.Refresh();
            gridView1.PopulateColumns();

            DevExpress.XtraGrid.Columns.GridColumn col = gridView1.Columns[19];
            col.DisplayFormat.FormatString = "yyyy-MM-dd HH:mm:ss";
            col.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom;
            gridView1.OptionsView.ColumnAutoWidth = false;
            gridView1.BestFitColumns();


            //if (filter)
            //{

            //button7.PerformClick();
            /*
            for (int i = 0; i < 10; i++) gridView1.Columns[i].Visible = false;
            for (int i = 11; i < 18; i++) gridView1.Columns[i].Visible = false;
            for (int i = 23; i < 28; i++) gridView1.Columns[i].Visible = false;
             * */
            //}

            for (int i = 0; i < 10; i++) gridView1.Columns[i].Visible = false;
            for (int i = 11; i < 18; i++) gridView1.Columns[i].Visible = false;
            for (int i = 25; i < 28; i++) gridView1.Columns[i].Visible = false;

            gridView1.Columns[14].Visible = true;
            gridView1.Columns[13].Visible = true;
            gridView1.Columns[12].Visible = true;


            //设置并制作Foot下面的统计信息
            gridView1.Columns[19].SummaryItem.DisplayFormat = "{0}";
            //gridView1.Columns[0].SummaryItem.FieldName = "analysis_review_id";
            gridView1.Columns[19].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Count;
        }
        //}

        //private void simpleButton2_Click(object sender, EventArgs eeeee)
        //{

        //}




        private void simpleButton7_Click(object sender, EventArgs eeee)
        {
            LoadDataMethod ldm = new LoadDataMethod();

            DataClassesMatchDataContext2 matches = new DataClassesMatchDataContext2(Conn.conn);

            var tables = matches.Analysis_Review
                               .Where(e => e.Match_time.Value > DateTime.Now.AddDays(-1))
                               .Where(e => filter_match_two.Contains(e.Match_type))  //上次赢了的类型
                //.Where(e => e.Data_wdl.Length != 3) //不等于3
                               .Where(e => e.bj_select == 2)
                               .OrderBy(e => e.Match_time);

            var tables1 = matches.Analysis_Review
                .Where(e => e.Match_time.Value > DateTime.Now.AddDays(-1))
                .Where(e => ldm.yesornorate.Contains(e.Match_type))
                .Where(e => filter_match.Contains(e.Match_type))
                .Where(e => e.bj_select == 1)
                .OrderBy(e => e.Match_time);

            //todayMatchType = new List<string>();
            //todayMatchType = tables.Select(e => e.Match_type).Distinct().ToList();

            gridControl1.DataSource = tables.Union(tables1).OrderBy(e => e.Match_time);
            gridControl1.Refresh();
            gridView1.PopulateColumns();

            DevExpress.XtraGrid.Columns.GridColumn col = gridView1.Columns[19];
            col.DisplayFormat.FormatString = "yyyy-MM-dd HH:mm:ss";
            col.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom;
            gridView1.OptionsView.ColumnAutoWidth = false;
            gridView1.BestFitColumns();


            for (int i = 0; i < 10; i++) gridView1.Columns[i].Visible = false;
            for (int i = 11; i < 18; i++) gridView1.Columns[i].Visible = false;
            for (int i = 23; i < 28; i++) gridView1.Columns[i].Visible = false;

            gridView1.Columns[26].Visible = true;


            //设置并制作Foot下面的统计信息
            gridView1.Columns[19].SummaryItem.DisplayFormat = "{0}";
            //gridView1.Columns[0].SummaryItem.FieldName = "analysis_review_id";
            gridView1.Columns[19].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Count;

        }




        private void button24_Click(object sender, EventArgs ee)
        {
            //getBjDangChang();
            DataClassesMatchDataContext2 matches = new DataClassesMatchDataContext2(Conn.conn);
            var tables = matches.Analysis_Review
                             .Where(e => e.Match_type == "歐國盃")
                             .Where(e => e.Match_time > DateTime.Now.AddDays(-60))
                               .OrderBy(e => e.Match_time);


            gridControl1.DataSource = tables;
            gridControl1.Refresh();
            gridView1.PopulateColumns();

            DevExpress.XtraGrid.Columns.GridColumn col = gridView1.Columns[19];
            col.DisplayFormat.FormatString = "yyyy-MM-dd HH:mm:ss";
            col.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom;
            gridView1.OptionsView.ColumnAutoWidth = false;
            gridView1.BestFitColumns();


            button7.PerformClick();


            //设置并制作Foot下面的统计信息
            gridView1.Columns[19].SummaryItem.DisplayFormat = "{0}";
            //gridView1.Columns[0].SummaryItem.FieldName = "analysis_review_id";
            gridView1.Columns[19].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Count;
        }






        private void simpleButton7_5Click(object sender, EventArgs eeee)
        {
            LoadDataMethod ldm = new LoadDataMethod();

            DataClassesMatchDataContext2 matches = new DataClassesMatchDataContext2(Conn.conn);

            var tables = matches.Analysis_Review
                               .Where(e => e.Match_time.Value > DateTime.Now.AddDays(-1))
                               .Where(e => filter_match_two.Contains(e.Match_type))  //上次赢了的类型
                //.Where(e => e.Data_wdl.Length != 3) //不等于3
                               .Where(e => e.bj_select == 2)
                               .OrderBy(e => e.Match_time);

            var tables1 = matches.Analysis_Review
                .Where(e => e.Match_time.Value > DateTime.Now.AddDays(-1))
                .Where(e => ldm.yesornorate.Contains(e.Match_type))
                .Where(e => filter_match.Contains(e.Match_type))
                .Where(e => e.bj_select == 1)
                .OrderBy(e => e.Match_time);

            //todayMatchType = new List<string>();
            //todayMatchType = tables.Select(e => e.Match_type).Distinct().ToList();

            gridControl1.DataSource = tables.Union(tables1).OrderBy(e => e.Match_time);
            gridControl1.Refresh();
            gridView1.PopulateColumns();

            DevExpress.XtraGrid.Columns.GridColumn col = gridView1.Columns[19];
            col.DisplayFormat.FormatString = "yyyy-MM-dd HH:mm:ss";
            col.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom;
            gridView1.OptionsView.ColumnAutoWidth = false;
            gridView1.BestFitColumns();


            for (int i = 0; i < 10; i++) gridView1.Columns[i].Visible = false;
            for (int i = 11; i < 18; i++) gridView1.Columns[i].Visible = false;
            for (int i = 23; i < 28; i++) gridView1.Columns[i].Visible = false;

            gridView1.Columns[26].Visible = true;


            //设置并制作Foot下面的统计信息
            gridView1.Columns[19].SummaryItem.DisplayFormat = "{0}";
            //gridView1.Columns[0].SummaryItem.FieldName = "analysis_review_id";
            gridView1.Columns[19].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Count;

        }



        private void button15_Click(object sender, EventArgs e)
        {
            HtmlAgilityPackGeneric t = new HtmlAgilityPackGeneric(webBrowser2.Document.Body.InnerHtml, textBox4.Text, Int32.Parse(textBox5.Text));
            dataGridView4.DataSource = t.GetTableInnerText_macau();
        }




        private void button24_5Click(object sender, EventArgs ee)
        {
            //getBjDangChang();
            DataClassesMatchDataContext2 matches = new DataClassesMatchDataContext2(Conn.conn);
            var tables = matches.Analysis_Review
                             .Where(e => e.Match_type == "歐國盃")
                             .Where(e => e.Match_time > DateTime.Now.AddDays(-60))
                               .OrderBy(e => e.Match_time);


            gridControl1.DataSource = tables;
            gridControl1.Refresh();
            gridView1.PopulateColumns();

            DevExpress.XtraGrid.Columns.GridColumn col = gridView1.Columns[19];
            col.DisplayFormat.FormatString = "yyyy-MM-dd HH:mm:ss";
            col.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom;
            gridView1.OptionsView.ColumnAutoWidth = false;
            gridView1.BestFitColumns();


            button7.PerformClick();


            //设置并制作Foot下面的统计信息
            gridView1.Columns[19].SummaryItem.DisplayFormat = "{0}";
            //gridView1.Columns[0].SummaryItem.FieldName = "analysis_review_id";
            gridView1.Columns[19].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Count;
        }
    }

}